const app = getApp()
const Controllers = require('../controllers/controllers').controller
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:'我的关注',
    list: [],
    isShowSelect: true, //是否显示选择按钮
    isShowSingleSelect: false, //是否点击当个选择按钮
    currentId: null, //当前歌曲的id
    lastPage: null,
    pageNumber: 1,
    totalRow: null,
    multipleList: [], //存储选中的id
    isSelected: 0,
    isMultipled: false, //是否已全选
  },
  // 显示多选按钮
  selectHandle() {
    this.setData({
      isShowSelect: !this.data.isShowSelect
    })
  },
  // 单个选择按钮
  singleSelectBtn(e) {
    let id = parseInt(e.currentTarget.dataset.id);
    let isMultipled = this.data.isMultipled; //是否全选
    let isSelected = this.data.isSelected; //已选多少个
    let multipleList = this.data.multipleList; //存储选中的id
    let list = this.data.list;
    // 判断是否已经选中
    if (multipleList.includes(id)) {
      // 选中的，就直接删除掉
      let idx = multipleList.indexOf(id);
      multipleList.splice(idx, 1)
      isSelected--
      isMultipled = false
    } else {
      multipleList.push(id)
      // 判断是否全选
      if (multipleList.length == list.length) {
        isMultipled = true
      }
      isSelected++
    }

    for (let item of list) {
      if (multipleList.includes(item.id)) {
        item.select = true
      } else {
        item.select = false
      }
    }
    console.log('单个选择按钮', id, isSelected)

    this.setData({
      isShowSingleSelect: true,
      currentId: id,
      multipleList,
      isSelected,
      isMultipled,
      list: list
    })
  },
  // 全选按钮
  multipleSelectBtn() {
    console.log('全选按钮')
    let multipleList = this.data.multipleList; //存储选中的id
    let isMultipled = this.data.isMultipled; //是否全选
    let isSelected = this.data.isSelected; //已选多少个
    let list = this.data.list;
    if (multipleList.length != 0) {
      if (multipleList.length == list.length) {
        multipleList.length = 0
        isMultipled = false
        isSelected = 0
        list.map(item => {
          item.select = false
        })
      } else {
        list.map(item => {
          if (!item.select) {
            item.select = true
            multipleList.push(item.id)
          }
        })
        isMultipled = true
        isSelected = list.length
      }
    } else {
      list.map(item => {
        item.select = true
        multipleList.push(item.id)
      })
      isMultipled = true
      isSelected = list.length
    }
    this.setData({
      multipleList,
      isMultipled,
      isSelected,
      list
    })
  },
  // 单个取消关注按钮
  UnsubscribeBtn(e) {
    let id = e.currentTarget.dataset.id
    console.log('取消关注按钮', id)
    Controllers.delFollow({
        followedUserIds: id
      })
      .then(res => {
        console.log(res)
        if (res.code == 0) {
          wx.showToast({
            title: '取消关注成功',
            duration: 1000
          })
          this.setData({
            list:[]
          })
          this.getFollowList();
        }
      }).catch(err => {
        console.log(err)
        wx.showToast({
          title: err.data.msg,
          duration: 1000,
          icon: 'none'
        })
      })
  },
  // 多个取消关注按钮
  multipledUnsubscribeBtn() {
    let multipleList = this.data.multipleList
    let str = multipleList.join(',')
    let that = this
    console.log(str)
    if (multipleList.length != 0) {
      // 弹出窗
      that.Dialog.ShowDialog({
        title: '是否取消关注用户',
        type: 'Confirm',
        callback(obj) {
          console.log('eee', obj)
          if (obj.name == 'confirm') {
            Controllers.delFollow({
                followedUserIds: str
              })
              .then(res => {
                console.log(res)
                if (res.code == 0) {
                  that.setData({
                    isShowSelect: true,
                    isSelected: 0,
                    multipleList: [],
                    pageNumber: 1,
                    list: [],
                    isMultipled: false
                  })
                  wx.showToast({
                    title: '取消关注成功',
                    duration: 1000
                  })
                  that.getFollowList();
                }
              }).catch(err => {
                console.log(err)
                wx.showToast({
                  title: err.data.msg,
                  duration: 1000,
                  icon: 'none'
                })
              })
          }
          that.Dialog.CloseDialog();
          that.setData({
            isShowSelect: true
          })
        }
      })

    } else {
      wx.showToast({
        title: '请选择用户',
        icon: 'none',
        duration: 1000
      })
    }
  },
  // 去找朋友
  toFindFriend() {
    console.log('去找朋友')
  },
  // 获取我关注的人列表数据
  getFollowList() {
    let that = this;
    let params = {
      pageNumber: this.data.pageNumber,
      token: wx.getStorageSync('logintoken')
    }
    Controllers.followList(params)
      .then(res => {
        console.log('获取我关注的人列表数据', res)
   
          let list = res.data.baseData.list;
          // res.data.baseData.list.map(item => {
          //   item.listenTimes = (item.listenTimes / 60 / 60).toFixed(2)
          //   list.push(item)
          // })
          that.setData({
            list:that.data.list.concat(list),
            lastPage: res.data.baseData.lastPage,
            pageNumber: res.data.baseData.pageNumber,
            totalRow: res.data.baseData.totalRow
          })

      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('onLoad')
    if (!wx.getStorageSync('logintoken')) {
      this.login();
    } else {
      this.getFollowList();
    }
  },
  login() {
    app.GetLogin()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.Dialog = this.selectComponent('#Dialog');
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('onShow')
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    if (!that.data.lastPage) {
      // 存在数据
      that.setData({
        pageNumber: that.data.pageNumber + 1
      })
      console.log('存在数据', that.data.pageNumber)
      that.getFollowList();

    } else {
      wx.showToast({
        title: '暂无数据',
        icon:"none"
      })
    }
  },

  onShareAppMessage: function (e) {
    // if (e.from == "button") {
      let obj = {
        title: '有氧FM·我的关注',
        success: function (res) {
          
        },
        fail(err) {
          
        }
      }
      return obj
    // }
  },
  onShareTimeline: function (e) {
    // if (e.from == "button") {
      let obj = {
        title: '有氧FM·我的关注',
        success: function (res) {
          
        },
        fail(err) {
          
        }
      }
      return obj
    // }
  },
})